﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"

void SetPos(short x, short y) {
	COORD pos = { x,y };
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(houtput, pos);
}


void WelcomeGame() {
	SetPos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(42, 14);
	wprintf(L"用↑.↓.←.→来控制蛇的移动，按F3加速，F4减速\n");
	SetPos(42, 15);
	wprintf(L"加速能获得更高的分数\n");
	SetPos(42, 20);
	system("pause");
	system("cls");

}

void CreatMap() {
	int i = 0;
	//上(0,0)-(56, 0) 
	SetPos(0, 0);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	SetPos(0, 26);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
	}
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%c", WALL);
	}
	

}

void InitSnake(psnake ps) {
	pSnakeNode cur = NULL;
	int i = 0;
	for (int i = 0; i < 5; i++) {
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL) {
			perror("InitSnake();;malloc");
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;

		if (ps->_pSnake == NULL) {
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	int j = 1;
	while (cur) {
		SetPos(cur->x, cur->y);
		if (j) {
			wprintf(L"%c", HEAD);
			j--;
		}
		else {
			wprintf(L"%c", BODY);
		}
		cur = cur->next;
	}
	//设置贪吃蛇属性
	ps->sorce = 0;
	ps->sleep_time = 200;
	ps->_dir = RIGHT;
	ps->_status = OK;
	ps->food_weight = 10;
	
}

void CreatFood(psnake ps) {
	int x = 0;
	int y = 0;
	//生成的x是2的倍数
	//x;2-54
	//y;1-25
again:
	do {
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakeNode cur = ps->_pSnake;
	while (cur) {
		if (x == cur->x && y == cur->y) {
			goto again;
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL) {
		perror("CreatFood()::malloc");
		return;
	}
	pFood->next = NULL;
	pFood->x = x;
	pFood->y = y;
	SetPos(pFood->x, pFood->y);
	wprintf(L"%c", FOOD);
	ps->_pFood = pFood;
}

void PrintfHelpInfo() {
	SetPos(64, 10);
	wprintf(L"%ls", L"不能穿墙，不能咬到自己");
	SetPos(64, 12);
	wprintf(L"%ls", L"用↑.↓.←.→来控制蛇的移动");
	SetPos(64, 13);
	wprintf(L"%ls", L"按F3加速，F4减速");
	SetPos(64, 14);
	wprintf(L"%ls", L"按ESC退出，按空格暂停游戏");
	SetPos(64, 16);
	wprintf(L"%ls", L"designer：无妄");
	SetPos(64, 20);
	printf("version:1.0");
}


//初始化游戏
//1.打印环境界面
//2.功能介绍
//3.绘制地图
//4打印蛇跟食物
//5.设置游戏功能信息
void GameStart(psnake ps) {
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO Cursorinfo;
	GetConsoleCursorInfo(houtput, &Cursorinfo);
	Cursorinfo.bVisible = false;
	SetConsoleCursorInfo(houtput, &Cursorinfo);

	WelcomeGame();
	CreatMap();

	InitSnake(ps);
	CreatFood(ps);

}


#define KEY_PRESS(vk) ((GetAsyncKeyState(vk)&0x1)?1:0)
void Pause() {
	while (1) {
		Sleep(200);
		if (KEY_PRESS(VK_SPACE)) {
			break;
		}
	}
}
int NextIsFood(pSnakeNode pn, psnake ps) {
	return (pn->x == ps->_pFood->x && pn->y == ps->_pFood->y);
}

void EatFood(pSnakeNode pn, psnake ps) {
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	free(pn);
	pn = NULL;
	pSnakeNode cur = ps->_pSnake;
	int j = 1;
	while (cur) {
		SetPos(cur->x, cur->y);
		SetPos(cur->x, cur->y);
		if (j) {
			wprintf(L"%c", HEAD);
			j--;
		}
		else {
			wprintf(L"%c", BODY);
		}
		cur = cur->next;
	}
	ps->sorce += ps->food_weight;
	//创建食物
	CreatFood(ps);
}
void NoFood(pSnakeNode pn, psnake ps) {
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;
	pSnakeNode cur = ps->_pSnake;
	int j = 1;
	while (cur->next->next != NULL) {
		SetPos(cur->x, cur->y);
		SetPos(cur->x, cur->y);
		if (j) {
			wprintf(L"%c", HEAD);
			j--;
		}
		else {
			wprintf(L"%c", BODY);
		}
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);
	printf(" ");
	free(cur->next);
	cur->next = NULL;
}
void KillByWall(psnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_status = KILL_BY_WALL;
	
	}
	
}

void KillBySelf(psnake ps) {
	pSnakeNode cur = ps->_pSnake->next;
	while (cur) {
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y) {
			ps->_status = KILL_BY_SELF;
			break;
		}
		else {
			cur = cur->next;
		}
	}

}
void SnakeMove(psnake ps) {
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if(pNextNode == NULL) {
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_dir) {
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y-1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x-2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x+2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	if (NextIsFood(pNextNode,ps)) {
		EatFood(pNextNode, ps);
	}
	else {
		NoFood(pNextNode,ps);
	}
	//检测蛇是否撞到自己跟墙
	KillByWall(ps);
	KillBySelf(ps);
}

//运行游戏
void GameRun(psnake ps) {
	//打印帮助信息
	PrintfHelpInfo();
	do {
		//打印总分数和食物的分值
		SetPos(64, 5);
		printf("总分数：%d", ps->sorce);
		SetPos(64, 6);
		printf("当前食物的分数：%d", ps->food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN) {
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP) {
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT) {
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT) {
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_SPACE)) {
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE)) {
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3)) {
			if (ps->sleep_time > 80)
			{
				ps->sleep_time -= 30;
				ps->food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4)) {
			if (ps->food_weight > 2) {
				ps->food_weight -= 2;
				ps->sleep_time += 30;
			}
		}

		SnakeMove(ps);
		Sleep(ps->sleep_time);
	} while (ps->_status == OK);
}
//结束游戏
void GameEnd(psnake ps) {
	SetPos(24, 12);
	switch (ps->_status) {
	case END_NORMAL:
		printf("你主动结束了游戏\n");
		break;
	case KILL_BY_SELF:
		printf("你撞到自己，游戏结束\n");
		break;
	case KILL_BY_WALL:
		printf("你撞到墙上，游戏结束\n");
		break;
	}
	pSnakeNode cur = ps->_pSnake;

	while (cur) {
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}

}